/**
 * Description
 *
 * Creation Date 21/12/2017
 * @author Candy
 */
import {
	AsyncStorage
} from 'react-native'
import * as types from './../actionTypes';
import { getStore, setStore, removeStore } from '../../utils/tools';
import { config } from '../../utils';
import { getUserById} from '../../api';

export const AddToCart = (productData) => {
	return async dispatch => {
		await getStore('cartList').then(cartList => {
			if(typeof cartList == 'string') {
				cartList = JSON.parse(cartList);
			}
			if(cartList && cartList[productData.product_id]) {
				cartList[productData.product_id]['num']++;
			} else {
				cartList[productData.product_id] = {
					'product_id': productData.product_id,
					'num': 1,
					'product_name': productData.product_name,
					'product_image': productData.product_image,
					'price': productData.price

				}
			}
			cartList = {...cartList}
			setStore('cartList', cartList);
			dispatch(AddProductsToCart(cartList));

		}).catch(err => {
			let cartList = {};
			cartList[productData.product_id] = {
				'product_id': productData.product_id,
				'num': 1,
				'product_name': productData.product_name,
				'product_image': productData.product_image,
				'price': productData.price

			};
			cartList = {...cartList}
			setStore('cartList', cartList);
			dispatch(AddProductsToCart(cartList));
		});
	}
};
export const ReduceToCart = (productData) => {
	return async dispatch => {
		await getStore('cartList').then(cartList => {
			if(typeof cartList == 'string') {
				cartList = JSON.parse(cartList);
			}
			if(!cartList) {
				cartList = null;
				removeStore('cartList');
			}
			if(cartList && cartList[productData.product_id]) {
				if(cartList[productData.product_id]['num'] > 1) {
					cartList[productData.product_id]['num']--;
				} else {
					cartList[productData.product_id] = {}
				}
				let dataList = [];
				for (let key in cartList) {
					if (cartList[key].product_id) {
						dataList[key] = cartList[key]
					}
				}
				cartList = {...dataList};
				setStore('cartList', cartList);
			}
			dispatch(AddProductsToCart(cartList));
		}).catch(err => {
			removeStore('cartList');
		});
	}
};

export const GetUserInfo = () => {
	return async dispatch => {
		await getStore('user_id').then(userId => {
			getUserById(userId).then(res => {
				let user = res.data;
				dispatch(LoginAction(user));
			})

		}).catch(err => {
			removeStore('user_id');
			console.log(err)
		});
	}
}

export const GoToPay = (totalPrice) => {
	return async dispatch => {
		await getStore('cartList').then(cartList => {
			if(typeof cartList == 'string') {
				cartList = JSON.parse(cartList);
			}
			cartList['total'] = totalPrice;
			cartList = {...cartList}
			dispatch(GoToPayProducts(cartList));

		}).catch(err => {
			console.log(err)
		});
	}
}

export const InitCart = () => {
	return async dispatch => {
		await getStore('cartList').then(cartList => {
			if(typeof cartList == 'string') {
				cartList = JSON.parse(cartList);
			}
			dispatch(initCartProducts(cartList));

		});
	}
};
export const removeCart = () => {
	return async dispatch => {
		await removeStore('cartList').then((res) => {
			dispatch(removeCartProducts());

		});
	}
};

export const LoginAction = (user) => {
	return {
		type: types.LOGIN,
		user,
		login: true
	}
};
export const addressList = (addressList) => {
	return {
		type: types.SAVE_ADDRESS,
		addressList
	}
};

//记录单个商品列表状态
export const AddProductsToCart = cartList => {
	return{
		type: types.ADD_CART,
		cartList
	}
};

export const initCartProducts = cartList => {
	return{
		type: types.INIT_CART,
		cartList
	}
};
export const removeCartProducts = () => {
	removeStore('cartList');
	return{
		type: types.REMOVE_CART,
		cartList: {},
		removeState: true
	}
};

//记录单个支付列表状态
export const GoToPayProducts = paymentList => {
	return{
		type: types.PAY_MENT,
		paymentList
	}
};